package utils;

import item.Obiekt;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedList;

public class NetworkLogger {
	private boolean disabled;

	public NetworkLogger() {
		this.disabled = false;
	}

	public NetworkLogger(boolean disabled) {
		this.disabled = disabled;
	}

	public void logAddItemToQueue(double time, Obiekt addedItem, LinkedList<Obiekt> queue, int id) {
		if (!disabled) {
			time = roundTime(time);
			StringBuilder komunikat = new StringBuilder(new String(""));
			komunikat.append("+K"+id);
			komunikat.append(" "+addedItem.dajID());
			komunikat.append("\t czas:" + time + "\t");
			komunikat.append("[" + dajItemyWSystemie(queue) + "] \t \trozmiar:"+queue.size());
			
			System.out.println(komunikat);
		}
	}

	public void logCarStartedToWash(double time, int id, Obiekt servicedItem, LinkedList<Obiekt> queue) {
		if (!disabled) {
			time = roundTime(time);
			
			StringBuilder komunikat = new StringBuilder(new String(""));
			komunikat.append("+M"+id);
			komunikat.append(" "+servicedItem.dajID());
			komunikat.append("\t czas:" + time);
			komunikat.append("\t[" + dajItemyWSystemie(queue) + "] rozmiar:"+queue.size());
			
			System.out.println(komunikat);
			
//			System.out.print("+M" + id + " " + servicedItem.dajID() + "\t" + time);
//			printBufor(queue);
		}
	}
	
	public void logCarWashed(double time, int id, Obiekt finishedItem, LinkedList<Obiekt> queue) {
		if (!disabled) {
			time = roundTime(time);
			
			StringBuilder komunikat = new StringBuilder(new String(""));
			komunikat.append("-M"+id);
			komunikat.append(" "+finishedItem.dajID());
			komunikat.append("\t czas:" + time);
			komunikat.append("\t[" + dajItemyWSystemie(queue) + "] rozmiar:"+queue.size());
			
			System.out.println(komunikat);
			
//			System.out.print("-M" + id + " " + finishedItem.dajID() + "\t" + time);
//			printBufor(queue);
		}
	}

	protected String dajItemyWSystemie(LinkedList<Obiekt> queue) {
		StringBuilder itemyWSystemie = new StringBuilder("");
		if (queue.size() > 0) {
			for (Obiekt item : queue) {
//				System.out.print(" " + item.dajID());
				itemyWSystemie.append(" " + item.dajID());
			}
		}
		return itemyWSystemie.toString();
	}

	protected double roundTime(double time) {
		time = new BigDecimal(time).round(new MathContext(6)).doubleValue();
		return time;
	}

}
